package com.cc.gulimall.seckill.controller;

import com.cc.gulimall.seckill.service.SeckillService;
import com.cc.gulimall.seckill.to.SeckillSkuRedisTo;
import com.cc.common.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>Title: SeckillController</p>
 * Description：
 * date：2020/7/6 22:24
 */
/*普通controller能够页面跳转
 * 普通的@ResponseBody才默认返回json数据
 * restcontroller不用加，默认返回json*/
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }
    // 1.进方法前后端判断是否登录，在拦截器里，登陆了再放行。
//    虽然已经有了前端拦截，但是这里是为了防止在postman或者网页地址栏直接发请求
    /*各种判断，保证秒杀单只创建一次*/
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
        String orderSn = seckillService.kill(killId, key, num);

        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
